//
//  GrowContentNoeditTableViewCell.m
//  XRSquare
//
//  Created by 穆相臣 on 2018/11/21.
//  Copyright © 2018 UaenaMo. All rights reserved.
//

#import "GrowContentNoeditTableViewCell.h"

@interface GrowContentNoeditTableViewCell ()
{
    UILabel * timeLabel;
    UIView * whiteBG;
    UILabel * typeLable;
    UILabel * contentTitle;
    UILabel * visiable;
    UILabel * contenttype;
    UILabel * contenttime;
    UILabel * posttime;
    UILabel * content;
    
    UIButton * deleteButton;
    
    UIImageView * image1;
    UIImageView * image2;
    UIImageView * image3;
    UIImageView * image4;
    UIImageView * image5;
    UIImageView * image6;
    UIImageView * image7;
    UIImageView * image8;
    UIImageView * image9;
    
    NSDictionary * contentData;
}
@end

@implementation GrowContentNoeditTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.backgroundColor = [UIColor clearColor];
    
        whiteBG = [[UIView alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth - 20, 30)];
        whiteBG.backgroundColor = [UIColor whiteColor];
        whiteBG.layer.cornerRadius = 10;
        [self addSubview:whiteBG];
        
        deleteButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth - 20 - 60, 10, 50, 30)];
        deleteButton.layer.cornerRadius = 10;
        deleteButton.layer.borderColor = themeColor.CGColor;
        deleteButton.layer.borderWidth = 1;
        deleteButton.clipsToBounds = YES;
        deleteButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [deleteButton setTitleColor:themeColor forState:UIControlStateNormal];
        [deleteButton addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
//        [whiteBG addSubview:deleteButton];
        
        typeLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth, 20)];
        typeLable.font = [UIFont systemFontOfSize:15];
        [whiteBG addSubview:typeLable];
        
        contentTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, ScreenWidth, 20)];
        contentTitle.font = [UIFont boldSystemFontOfSize:15];
        [whiteBG addSubview:contentTitle];
        
        visiable = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, ScreenWidth - 20 - 20, 20)];
        visiable.font = [UIFont systemFontOfSize:15];
        visiable.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        visiable.textAlignment = NSTextAlignmentRight;
        visiable.userInteractionEnabled = YES;
//        [whiteBG addSubview:visiable];
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editAction)];
        [visiable addGestureRecognizer:tap];
        
        UIImageView * tagImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 70, 20, 20)];
        tagImage.image = [UIImage imageNamed:@"记录属性"];
        [whiteBG addSubview:tagImage];

        contenttype = [[UILabel alloc]initWithFrame:CGRectMake(35, 70, ScreenWidth, 20)];
        contenttype.font = [UIFont systemFontOfSize:13];
        contenttype.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        [whiteBG addSubview:contenttype];
        
        UIImageView * tagImage1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 100, 20, 20)];
        tagImage1.image = [UIImage imageNamed:@"时间"];
        [whiteBG addSubview:tagImage1];
        
        contenttime = [[UILabel alloc]initWithFrame:CGRectMake(35, 100, ScreenWidth, 20)];
        contenttime.font = [UIFont systemFontOfSize:13];
        contenttime.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        [whiteBG addSubview:contenttime];
        
        UIImageView * tagImage2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 130, 20, 20)];
        tagImage2.image = [UIImage imageNamed:@"时间"];
        [whiteBG addSubview:tagImage2];
        
        posttime = [[UILabel alloc]initWithFrame:CGRectMake(35, 130, ScreenWidth, 20)];
        posttime.font = [UIFont systemFontOfSize:13];
        posttime.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        [whiteBG addSubview:posttime];
        
        content = [[UILabel alloc]initWithFrame:CGRectMake(10, 160, ScreenWidth - 20 - 20, 20)];
        content.font = [UIFont systemFontOfSize:15];
        content.numberOfLines = 0;
        [whiteBG addSubview:content];
        
        image1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, content.frame.origin.y + content.frame.size.height + 10, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3)];
        [whiteBG addSubview:image1];
        
        image2 = [[UIImageView alloc]initWithFrame:CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3)];
        [whiteBG addSubview:image2];
        
        image3 = [[UIImageView alloc]initWithFrame:CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10, 20, 20)];
        [whiteBG addSubview:image3];
        
        image4 = [[UIImageView alloc]initWithFrame:CGRectMake(10, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3)];
        [whiteBG addSubview:image4];
        
        image5 = [[UIImageView alloc]initWithFrame:CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3)];
        [whiteBG addSubview:image5];
        
        image6 = [[UIImageView alloc]initWithFrame:CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5, 20, 20)];
        [whiteBG addSubview:image6];
        
        image7 = [[UIImageView alloc]initWithFrame:CGRectMake(10, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3)];
        [whiteBG addSubview:image7];
        
        image8 = [[UIImageView alloc]initWithFrame:CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3)];
        [whiteBG addSubview:image8];
        
        image9 = [[UIImageView alloc]initWithFrame:CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3)];
        [whiteBG addSubview:image9];
        image1.contentMode = UIViewContentModeScaleAspectFill;
        image1.clipsToBounds = YES;
        image2.contentMode = UIViewContentModeScaleAspectFill;
        image2.clipsToBounds = YES;
        image3.contentMode = UIViewContentModeScaleAspectFill;
        image3.clipsToBounds = YES;
        image4.contentMode = UIViewContentModeScaleAspectFill;
        image4.clipsToBounds = YES;
        image5.contentMode = UIViewContentModeScaleAspectFill;
        image5.clipsToBounds = YES;
        image6.contentMode = UIViewContentModeScaleAspectFill;
        image6.clipsToBounds = YES;
        image7.contentMode = UIViewContentModeScaleAspectFill;
        image7.clipsToBounds = YES;
        image8.contentMode = UIViewContentModeScaleAspectFill;
        image8.clipsToBounds = YES;
        image9.contentMode = UIViewContentModeScaleAspectFill;
        image9.clipsToBounds = YES;
        
        UITapGestureRecognizer * tapAction = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImageAction)];
        [image1 addGestureRecognizer:tapAction];
        [image2 addGestureRecognizer:tapAction];
        [image3 addGestureRecognizer:tapAction];
        [image4 addGestureRecognizer:tapAction];
        [image5 addGestureRecognizer:tapAction];
        [image6 addGestureRecognizer:tapAction];
        [image7 addGestureRecognizer:tapAction];
        [image8 addGestureRecognizer:tapAction];
        [image9 addGestureRecognizer:tapAction];
        
        image1.userInteractionEnabled = YES;
        image2.userInteractionEnabled = YES;
        image3.userInteractionEnabled = YES;
        image4.userInteractionEnabled = YES;
        image5.userInteractionEnabled = YES;
        image6.userInteractionEnabled = YES;
        image7.userInteractionEnabled = YES;
        image8.userInteractionEnabled = YES;
        image9.userInteractionEnabled = YES;
        
        whiteBG.frame = CGRectMake(10, 50, ScreenWidth - 20, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 10);
    }
    return self;
}

+(CGFloat)heightwithData:(NSDictionary *)data
{
    UILabel * conten = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - 40, 20)];
    conten.font = [UIFont systemFontOfSize:15];
    conten.numberOfLines = 0;
    conten.text = [data objectForKey:@"Content"];
    [conten sizeToFit];
    
    
    NSString * pic = [data objectForKey:@"Pic"];
    if ([pic isKindOfClass:[NSNull class]])
    {
        return 10 + 160 + conten.frame.size.height + 10;
    }
    else
    {
        NSData * jspic = [pic dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * picarr = [NSJSONSerialization JSONObjectWithData:jspic options:NSJSONReadingMutableLeaves error:nil];
        if ([picarr count] < 3)
        {
            return 10 + 160 + conten.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 10;
        }
        else if ([picarr count]< 6)
        {
            return 10 + 160 + conten.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 10;
        }
        else
        {
            return 10 + 160 + conten.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 10;
        }
    }
}

-(void)loadData:(NSDictionary *)data
{
    
    contentData = data;
    
    int type = [[data objectForKey:@"Type"]intValue];
    
    timeLabel.text = [data objectForKey:@"CommitDate"];
    
    if (type == 1)
    {
        typeLable.text = @"个人采风";
    }
    else if (type == 2)
    {
        typeLable.text = @"校园活动";
    }
    else if (type == 3)
    {
        typeLable.text = @"社会活动";
    }
    else if (type == 4)
    {
        typeLable.text = @"技能资质";
    }
    else if (type == 5)
    {
        typeLable.text = @"梦想舞台";
    }
    else if (type == 0)
    {
        typeLable.text = @"个人大事记";
    }
    
    contentTitle.text = [data objectForKey:@"Title"];
    
    int visiablenum = [[data objectForKey:@"ShowType"]intValue];
    if (visiablenum == 0) {
        visiable.text = @"所有人";
    }
    else if (visiablenum == 1)
    {
        visiable.text = @"仅自己";
    }
    else if (visiablenum == 2)
    {
        visiable.text = @"好友";
    }
    else
    {
        visiable.text = @"全部";
    }
    
    if ([data objectForKey:@"Property"])
    {
        contenttype.text = [NSString stringWithFormat:@"记录属性 %@",[data objectForKey:@"Property"]];
    }
    else
    {
        contenttype.text = typeLable.text;
    }
    contenttime.text = [NSString stringWithFormat:@"%@",[data objectForKey:@"EventTime"]];
    posttime.text = [NSString stringWithFormat:@"%@",[data objectForKey:@"CommitTime"]];
    
    content.frame = CGRectMake(10, 160, ScreenWidth - 20 - 20, 20);
    content.text = [data objectForKey:@"Content"];
    [content sizeToFit];
    
    image1.hidden = YES;
    image2.hidden = YES;
    image3.hidden = YES;
    image4.hidden = YES;
    image5.hidden = YES;
    image6.hidden = YES;
    image7.hidden = YES;
    image8.hidden = YES;
    image9.hidden = YES;
    
    NSString * pic = [data objectForKey:@"Pic"];
    if ([pic isKindOfClass:[NSNull class]])
    {
        
        whiteBG.frame = CGRectMake(10, 10, ScreenWidth - 20, content.frame.origin.y + content.frame.size.height + 10);
    }
    else
    {
        NSData * jspic = [pic dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * picarr = [NSJSONSerialization JSONObjectWithData:jspic options:NSJSONReadingMutableLeaves error:nil];
        switch ([picarr count]) {
            case 9:
                image9.hidden = NO;
                [image9 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[picarr objectAtIndex:8]]]];
            case 8:
                image8.hidden = NO;
                [image8 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[picarr objectAtIndex:7]]]];
            case 7:
                image7.hidden = NO;
                [image7 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[picarr objectAtIndex:6]]]];
            case 6:
                image6.hidden = NO;
                [image6 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[picarr objectAtIndex:5]]]];
            case 5:
                image5.hidden = NO;
                [image5 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[picarr objectAtIndex:4]]]];
            case 4:
                image4.hidden = NO;
                [image4 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[picarr objectAtIndex:3]]]];
            case 3:
                image3.hidden = NO;
                [image3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[picarr objectAtIndex:2]]]];
            case 2:
                image2.hidden = NO;
                [image2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[picarr objectAtIndex:1]]]];
            case 1:
                image1.hidden = NO;
                [image1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[picarr objectAtIndex:0]]]];
            default:
                break;
        }
        if ([picarr count] < 3)
        {
            whiteBG.frame = CGRectMake(10, 10, ScreenWidth - 20, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 10);
        }
        else if ([picarr count]< 6)
        {
            whiteBG.frame = CGRectMake(10, 10, ScreenWidth - 20, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 10);
        }
        else
        {
            whiteBG.frame = CGRectMake(10, 10, ScreenWidth - 20, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 10);
        }
    }
    
    image1.frame = CGRectMake(10, content.frame.origin.y + content.frame.size.height + 10, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3);
    image2.frame = CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3);
    image3.frame = CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3);
    image4.frame = CGRectMake(10, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3);
    image5.frame = CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3);
    image6.frame = CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3);
    image7.frame = CGRectMake(10, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3);
    image8.frame = CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3);
    image9.frame = CGRectMake(10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, content.frame.origin.y + content.frame.size.height + 10 + (ScreenWidth - 40 - 10)/3 + 5 + (ScreenWidth - 40 - 10)/3 + 5, (ScreenWidth - 40 - 10)/3, (ScreenWidth - 40 - 10)/3);
}

-(void)hideDel
{
    deleteButton.hidden = YES;
}

-(void)showImageAction
{
    NSString * pic = [contentData objectForKey:@"Pic"];
    NSData * jspic = [pic dataUsingEncoding:NSUTF8StringEncoding];
    NSArray * picarr = [NSJSONSerialization JSONObjectWithData:jspic options:NSJSONReadingMutableLeaves error:nil];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

